package com.ycfmedical.bed.base;

import android.os.Bundle;
import android.view.View;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.local.entity.HisSystemInfo;
import com.ycfmedical.bed.present.HisPresenter;
import com.ycfmedical.bed.widgets.StateView;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by Administrator on 2017/3/16 0016.
 */

public abstract class BaseHisFragment extends XFragment<HisPresenter>
{
    @BindView(R.id.his_contentLayout)
    XRecyclerContentLayout hisContentLayout;
    StateView errorView;

    @Override
    public void initData(Bundle savedInstanceState)
    {
        initAdapter();
        getP().loadData(Constants.DEFAULT_PAGE, getType());
    }

    private void initAdapter()
    {
        setLayoutManager(hisContentLayout.getRecyclerView());

        hisContentLayout.getRecyclerView().setAdapter(getAdapter());

        hisContentLayout.getRecyclerView()
            .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener()
            {
                @Override
                public void onRefresh()
                {
//                     getP().handleMessage(Constants.DEFAULT_PAGE, getType());
                }

                @Override
                public void onLoadMore(int page)
                {
                    // getP().handleMessage(page, getType());
                }

            });

        if (errorView == null)
        {
            errorView = new StateView(context);
        }

        hisContentLayout.errorView(errorView);
        hisContentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        hisContentLayout.getRecyclerView().useDefLoadMoreView();
    }

    public void showError(NetError error)
    {
        if (error != null)
        {
            switch (error.getType())
            {
                case NetError.ParseError:
                    errorView.setMsg("数据解析异常");
                    break;

                case NetError.AuthError:
                    errorView.setMsg("身份验证异常");
                    break;

                case NetError.BusinessError:
                    errorView.setMsg("业务异常");
                    break;

                case NetError.NoConnectError:
                    errorView.setMsg("网络无连接");
                    break;

                case NetError.NoDataError:
                    errorView.setMsg("数据为空");
                    break;

                case NetError.OtherError:
                    errorView.setMsg("其他异常");
                    break;
            }
            hisContentLayout.showError();
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_base_his;
    }

    @Override
    public HisPresenter newP()
    {
        return new HisPresenter();
    }

    // 获得Fragemnt的种类
    public abstract int getType();

    public abstract SimpleRecAdapter getAdapter();

    public abstract void setLayoutManager(XRecyclerView recyclerView);

    public void showData(List<HisSystemInfo> hisSystemInfos)
    {
        int size = hisSystemInfos.size();
        getAdapter().setData(hisSystemInfos);
    }
}
